<?php
/**
 * 访客统计表
 *
 * Author: Zeon
 * Date: 2018/4/19
 * Created by Panxsoft.
 */
namespace App\Models\Panx;

use App\Models\Concerns\ScopeFilterable;
use App\Models\Customer\DeviceAccount;
use App\Models\OpenWechat\WechatUser;
use App\Models\Store\Store;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * @property int hostable_id
 * @property int visitorable_id
 * @property string visitorable_type
 * @property string hostable_type
 *
 * @property-read Model|WechatUser visitorable
 * @property-read Model|Store hostable
 *
 * @mixin Builder
 */
class Visit extends Model
{
    use ScopeFilterable;

    protected $table = 'visits';

    protected static $unguarded = true;

    protected $casts = [
        'visitorable_id' => 'int',
        'hostable_id'    => 'int',
        'data'           => 'json',
    ];

    /**
     * 微信用户扫码访问店铺
     *
     * @param int $wechat_user_id
     * @param int $store_id
     * @param array|null $data
     * @return Visit
     */
    public static function createWechatUserVisitStore(int $wechat_user_id, int $store_id, array $data = null): self
    {
        return (new self)->create([
            'visitorable_id'   => $wechat_user_id,
            'visitorable_type' => class_basename(WechatUser::class),
            'hostable_id'      => $store_id,
            'hostable_type'    => class_basename(Store::class),
            'description'      => '微信用户扫码访问店铺',
            'data'             => $data,
            'type'             => VisitType::WECHAT_USER_VISIT_STORE,
        ]);
    }

    /**
     * 微信用户扫码访问店铺
     *
     * @param int $product_id
     * @param int $device_account_id
     * @param array|null $data
     *
     * @return Visit
     */
    public static function createAppScanProduct(int $product_id, int $device_account_id, array $data = null): self
    {
        return (new self)->create([
            'visitorable_id'   => $product_id,
            'visitorable_type' => class_basename(Product::class),
            'hostable_id'      => $device_account_id,
            'hostable_type'    => class_basename(DeviceAccount::class),
            'description'      => 'APP机器扫码跳转外链',
            'data'             => $data,
            'type'             => VisitType::APP_SCAN_PROD_LINK_URL,
        ]);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function visitorable()
    {
        return $this->morphTo('visitorable', 'visitorable_type', 'visitorable_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function hostable()
    {
        return $this->morphTo('hostable', 'hostable_type', 'hostable_id', 'id');
    }

    /**
     * @return null|string
     */
    public function getVisitorNameAttribute(): ?string
    {
        $visitor = $this->visitorable;

        if ($visitor instanceof WechatUser) {
            return $visitor->nickname;
        }

        return null;
    }

    /**
     * @return null|string
     */
    public function getHostNameAttribute(): ?string
    {
        $host = $this->hostable;

        if ($host instanceof Store) {
            return $host->name;
        }

        return null;
    }
}
